package com.example.service.impl;

import com.example.domain.entity.Product;
import com.example.mapper.ProductMapper;
import com.example.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.example.domain.query.ProductQuery;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.StringUtils;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2025-05-07
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Override
    public IPage<Product> pageQuery(ProductQuery query, int pageNum, int pageSize) {
        Page<Product> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if (query != null) {
            if (query.getProductId() != null) {
                wrapper.eq("product_id", query.getProductId());
            }
            if (StringUtils.hasText(query.getProductName())) {
                wrapper.like("product_name", query.getProductName());
            }
            if (StringUtils.hasText(query.getAppearanceSize())) {
                wrapper.like("appearance_size", query.getAppearanceSize());
            }
            if (StringUtils.hasText(query.getBatch())) {
                wrapper.like("batch", query.getBatch());
            }
            if (StringUtils.hasText(query.getOperator())) {
                wrapper.like("operator", query.getOperator());
            }
            if (StringUtils.hasText(query.getProcessMethod())) {
                wrapper.like("process_method", query.getProcessMethod());
            }
            if (query.getProcessBeginTime() != null && query.getProcessEndTime() != null) {
                wrapper.between("process_time", query.getProcessBeginTime(), query.getProcessEndTime());
            }
            if (StringUtils.hasText(query.getSurfaceWorkshop())) {
                wrapper.like("surface_workshop", query.getSurfaceWorkshop());
            }
            if (StringUtils.hasText(query.getForgingWorkshop())) {
                wrapper.like("forging_workshop", query.getForgingWorkshop());
            }
            if (StringUtils.hasText(query.getAssemblyWorkshop())) {
                wrapper.like("assembly_workshop", query.getAssemblyWorkshop());
            }
            if (StringUtils.hasText(query.getQualityInspection())) {
                wrapper.like("quality_inspection", query.getQualityInspection());
            }
        }
        return this.page(page, wrapper);
    }
}
